/*
 *  Copyright 2004 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *
 */
#include <std.h>
#include <tsk.h>
#include <sem.h>
#include <gio.h>

#include <csl_dat.h>
#include <csl_cache.h>


#include <fvid.h>
#include <edc.h>
#include <vport.h>
#include <vportcap.h>
#include <vportdis.h>
#include <saa7105.h>
#include <tvp51xx.h>

#include <csl_vphal.h>

#include <evmdm642.h>
#include <evmdm642_capParamsSDTVDefault.h>
#include <evmdm642_disParamsSDTVDefault.h>

#include "colorbar.h"

/*****************************************************************************
* Here only the BT.656 embedded sync mode is illustrated, which uses EAV and
* SAV codes that are embedded in the data stream.
* To test external sync mode, which uses vsync and hsync signals, change:
* EVMDM642_CAP_PARAMS_PORT_EMBEDDED_DEFAULT,
* EVMDM642_CAP_PARAMS_CHAN_EMBEDDED_DEFAULT,
* EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT
* to:
* EVMDM642_CAP_PARAMS_PORT_EXTERNAL_DEFAULT,
* EVMDM642_CAP_PARAMS_CHAN_EXTERNAL_DEFAULT,
* EVMDM642_CAP_PARAMS_TVP51XX_EXTERNAL_DEFAULT
******************************************************************************/

/* caputure configuration parameters */
/* embedded sync mode is recommended as it offers better re-sync capability */
VPORT_PortParams EVMDM642_vCapParamsPort
  = EVMDM642_CAP_PARAMS_PORT_EMBEDDED_DEFAULT;

#if defined _NTSC

static VPORTCAP_Params EVMDM642_vCapParamsChan
  = EVMDM642_CAP_PARAMS_CHAN_EMBEDDED_DEFAULT(NTSC720);


#ifdef _LOOPBACK
/* For the vp0 & tvp5146, the port number must be fixed at 0 */
/* Analog input format can be either COMPOSITE or S-Video           */
static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5146
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(NTSC601, COMPOSITE, 0);

#ifdef _PIP
/* For the vp1 & tvp5150, port 1 selects the connector J17, while
   0 selects the connector J18.
   Analog input format can only be composite.
*/
static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5150
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(NTSC601, COMPOSITE, 1);
#endif

#endif
#elif defined _PAL

static VPORTCAP_Params EVMDM642_vCapParamsChan
  = EVMDM642_CAP_PARAMS_CHAN_EMBEDDED_DEFAULT(PAL720);

#ifdef _LOOPBACK

/* For the vp0 & tvp5146, the port number must be fixed at 0 */
static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5146
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(PAL601, COMPOSITE,0);
#ifdef _PIP
/* For the vp1 & tvp5150, port 1 selects the connector J17, while
   0 selects the connector J18.
   Analog input format can only be composite.
*/
static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5150
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(PAL601, COMPOSITE, 1);
#endif

#endif
#endif

/* display configuration parameters */
VPORT_PortParams EVMDM642_vDisParamsPort
  = EVMDM642_DIS_PARAMS_PORT_DEFAULT;

#if defined _NTSC

static VPORTDIS_Params EVMDM642_vDisParamsChan
  = EVMDM642_DIS_PARAMS_CHAN_BT656_DEFAULT(NTSC);

static SAA7105_ConfParams EVMDM642_vDisParamsSAA7105
  = EVMDM642_DIS_PARAMS_SAA7105_SDTV_DEFAULT(NTSC720, SVIDEO);

#elif defined _PAL

static VPORTDIS_Params EVMDM642_vDisParamsChan
  = EVMDM642_DIS_PARAMS_CHAN_BT656_DEFAULT(PAL);

static SAA7105_ConfParams EVMDM642_vDisParamsSAA7105
  = EVMDM642_DIS_PARAMS_SAA7105_SDTV_DEFAULT(PAL720, SVIDEO);
#endif

/* heap IDs defined in the BIOS configuration file */
extern Int EXTERNALHEAP;

/*
 * ======== main ========
 */
main()
{
    /******************************************************/
    /* open CSL DAT module for fast copy                  */
    /******************************************************/
    CSL_init();
    CACHE_clean(CACHE_L2ALL, 0, 0);
    CACHE_setL2Mode(CACHE_256KCACHE);
    CACHE_enableCaching(CACHE_EMIFA_CE00);
    CACHE_enableCaching(CACHE_EMIFA_CE01);
    DAT_open(DAT_CHAANY, DAT_PRI_LOW, DAT_OPEN_2D);

}

/*
 * ======== tskVideoLoopback ========
 * video loopback function.
 */
void tskVideoLoopback()
{
    Int status;
    FVID_Handle disChan;
    Int frames = 0;
    FVID_Frame *disFrameBuf;
    Int numLinesDis = EVMDM642_vDisParamsChan.imgVSizeFld1;
    Int numLinesCap = EVMDM642_vCapParamsChan.fldYStop1 -
       EVMDM642_vCapParamsChan.fldYStrt1+1;


    Int numLines = (numLinesDis > numLinesCap) ? numLinesCap : numLinesDis;

#ifdef _LOOPBACK
    Int i;
    FVID_Handle capChan;
    Int numPixels = EVMDM642_vCapParamsChan.fldXStop1 -
       EVMDM642_vCapParamsChan.fldXStrt1+1;
    FVID_Frame *capFrameBuf;
    Int capLinePitch = EVMDM642_vCapParamsChan.fldXStop1 -
       EVMDM642_vCapParamsChan.fldXStrt1+1;
    Int disLinePitch = EVMDM642_vDisParamsChan.imgHSizeFld1;


#ifdef _PIP
    VPORTCAP_Params EVMDM642_vCapParamsChan2 = EVMDM642_vCapParamsChan;
    FVID_Handle capChan2;
    FVID_Frame *capFrameBuf2;
    Int yPitch = capLinePitch >> 1;
    Int cPitch = ((capLinePitch >> 2) + 7) & (~ 7);

#endif
#endif


    numLines *= 2; /* both fields */
    /******************************************************/
    /* allocate both capture and display frame buffers    */
    /* in external heap memory                            */
    /******************************************************/
    EVMDM642_vCapParamsChan.segId = EXTERNALHEAP;
    EVMDM642_vDisParamsChan.segId = EXTERNALHEAP;
    EVMDM642_vDisParamsSAA7105.hI2C = EVMDM642_I2C_hI2C;


#ifdef _LOOPBACK
    EVMDM642_vCapParamsTVP5146.hI2C = EVMDM642_I2C_hI2C;

    /******************************************************/
    /* initialization of capture driver                   */
    /******************************************************/
    capChan = FVID_create("/VP0CAPTURE/A/0",
            IOM_INPUT, &status, (Ptr)&EVMDM642_vCapParamsChan, NULL);

#ifdef _PIP
    EVMDM642_vCapParamsTVP5150.hI2C = EVMDM642_I2C_hI2C;
    EVMDM642_vCapParamsChan2.scale = VPORT_SCALING_ENABLE;
    EVMDM642_vCapParamsChan2.fldOp = VPORT_FLDOP_FLD1;
    EVMDM642_vCapParamsChan2.thrld >>=1;
    capChan2 = FVID_create("/VP1CAPTURE/A/1",
            IOM_INPUT, &status, (Ptr)&EVMDM642_vCapParamsChan2, NULL);
#endif

#endif
    /******************************************************/
    /* initialization of display driver                   */
    /******************************************************/
    disChan = FVID_create("/VP2DISPLAY", IOM_OUTPUT,
        &status, (Ptr)&EVMDM642_vDisParamsChan, NULL);


    /******************************************************/
    /* configure video encoder & decoder                  */
    /******************************************************/
    FVID_control(disChan, VPORT_CMD_EDC_BASE + EDC_CONFIG,
        (Ptr)&EVMDM642_vDisParamsSAA7105);
#ifdef _LOOPBACK
    FVID_control(capChan, VPORT_CMD_EDC_BASE + EDC_CONFIG,
        (Ptr)&EVMDM642_vCapParamsTVP5146);
#ifdef _PIP
    FVID_control(capChan2, VPORT_CMD_EDC_BASE+EDC_CONFIG,
        (Ptr)&EVMDM642_vCapParamsTVP5150);
#endif
#endif


    /******************************************************/
    /* start capture & display operation                  */
    /******************************************************/
    FVID_control(disChan, VPORT_CMD_START, NULL);

#ifdef _LOOPBACK
    FVID_control(capChan, VPORT_CMD_START, NULL);
#ifdef _PIP
    FVID_control(capChan2, VPORT_CMD_START, NULL);
#endif
#endif

    /********************************************************/
    /* request a frame buffer from display & capture driver */
    /********************************************************/
    FVID_alloc(disChan, &disFrameBuf);
#ifdef _LOOPBACK
    FVID_alloc(capChan, &capFrameBuf);
#ifdef _PIP
    FVID_alloc(capChan2, &capFrameBuf2);
#endif
#endif


    while(1){/* loop for 4 seconds */
#ifdef _LOOPBACK

        /* copy data from capture buffer to display buffer */
        /***************************************************/
        for(i = 0; i < numLines; i ++) {
            DAT_copy(capFrameBuf->frame.iFrm.y1 + i * capLinePitch,
                     disFrameBuf->frame.iFrm.y1 + i * disLinePitch,
                     numPixels);
            DAT_copy(capFrameBuf->frame.iFrm.cb1 + i * (capLinePitch >> 1),
                     disFrameBuf->frame.iFrm.cb1 + i * (disLinePitch >> 1),
                     numPixels>>1);

            DAT_copy(capFrameBuf->frame.iFrm.cr1 + i * (capLinePitch >> 1),
                     disFrameBuf->frame.iFrm.cr1 + i * (disLinePitch >> 1),
                     numPixels>>1);
        }

#ifdef _PIP
        for(i = 0; i < (numLines>>1); i ++) {
            DAT_copy(capFrameBuf2->frame.iFrm.y1 + i * yPitch,
                     disFrameBuf->frame.iFrm.y1 + i * disLinePitch
                     + (disLinePitch >> 1),
                     (numPixels>>1));
            DAT_copy(capFrameBuf2->frame.iFrm.cb1 + i * cPitch,
                     disFrameBuf->frame.iFrm.cb1 + i * (disLinePitch >> 1)
                     + (disLinePitch >>2 ),
                     (numPixels >> 2));

            DAT_copy(capFrameBuf2->frame.iFrm.cr1 + i * cPitch,
                     disFrameBuf->frame.iFrm.cr1 + i * (disLinePitch >> 1)
                     +(disLinePitch >> 2),
                     (numPixels >> 2));
        }
#endif
        DAT_wait(DAT_XFRID_WAITALL);
        FVID_exchange(capChan, &capFrameBuf);

#ifdef _PIP
        FVID_exchange(capChan2, &capFrameBuf2);
#endif
#else
        fillFrmBuf(&disFrameBuf->frame.iFrm, EVMDM642_vDisParamsChan.imgHSizeFld1,
          EVMDM642_vDisParamsChan.imgVSizeFld1
            + EVMDM642_vDisParamsChan.imgVSizeFld2,
          frames % 360);
#endif
        FVID_exchange(disChan, &disFrameBuf);
        frames ++;
    }
}
